using DocumentCreationSystem.Models.AgentOps;
using DocumentCreationSystem.Models;
using DocumentCreationSystem.Models.Agentic;

namespace DocumentCreationSystem.Services.AgentOps
{
    /// <summary>
    /// AgentOps集成服务接口 - 智能规划系统与Agentic对话的联动接口
    /// </summary>
    public interface IAgentOpsIntegrationService
    {
        /// <summary>
        /// 初始化集成服务
        /// </summary>
        Task InitializeAsync();

        /// <summary>
        /// 从智能规划结果创建工作流
        /// </summary>
        /// <param name="planningResult">智能规划结果</param>
        /// <param name="context">创建上下文</param>
        /// <returns>创建的工作流</returns>
        Task<AgentWorkflow> CreateWorkflowFromPlanningAsync(PlanningResult planningResult, WorkflowCreationContext context);

        /// <summary>
        /// 启动智能规划驱动的创作流程
        /// </summary>
        /// <param name="userRequest">用户请求</param>
        /// <param name="projectId">项目ID</param>
        /// <param name="progressCallback">进度回调</param>
        /// <returns>集成执行结果</returns>
        Task<IntegratedExecutionResult> StartPlanningDrivenCreationAsync(
            string userRequest, 
            string? projectId = null,
            IProgress<IntegratedProgress>? progressCallback = null);

        /// <summary>
        /// 同步工作流状态到对话窗口
        /// </summary>
        /// <param name="workflowId">工作流ID</param>
        /// <param name="dialogSessionId">对话会话ID</param>
        Task SyncWorkflowStatusToDialogAsync(string workflowId, string dialogSessionId);

        /// <summary>
        /// 从对话窗口控制工作流
        /// </summary>
        /// <param name="workflowId">工作流ID</param>
        /// <param name="command">控制命令</param>
        /// <param name="parameters">命令参数</param>
        /// <returns>控制结果</returns>
        Task<WorkflowControlResult> ControlWorkflowFromDialogAsync(
            string workflowId, 
            WorkflowControlCommand command, 
            Dictionary<string, object>? parameters = null);

        /// <summary>
        /// 获取集成状态概览
        /// </summary>
        /// <returns>集成状态信息</returns>
        Task<IntegrationStatusOverview> GetIntegrationStatusAsync();

        /// <summary>
        /// 注册工作流事件到对话系统
        /// </summary>
        /// <param name="workflowId">工作流ID</param>
        /// <param name="dialogSessionId">对话会话ID</param>
        Task RegisterWorkflowEventsToDialogAsync(string workflowId, string dialogSessionId);

        /// <summary>
        /// 生成智能建议
        /// </summary>
        /// <param name="context">当前上下文</param>
        /// <returns>智能建议列表</returns>
        Task<List<SmartSuggestion>> GenerateSmartSuggestionsAsync(IntegrationContext context);

        /// <summary>
        /// 执行智能决策
        /// </summary>
        /// <param name="decisionRequest">决策请求</param>
        /// <returns>决策结果</returns>
        Task<SmartDecisionResult> ExecuteSmartDecisionAsync(SmartDecisionRequest decisionRequest);

        /// <summary>
        /// 获取实时监控数据
        /// </summary>
        /// <param name="scope">监控范围</param>
        /// <returns>监控数据</returns>
        Task<MonitoringData> GetRealTimeMonitoringDataAsync(MonitoringScope scope);

        /// <summary>
        /// 注册集成事件监听器
        /// </summary>
        /// <param name="listener">事件监听器</param>
        void RegisterEventListener(IIntegrationEventListener listener);

        /// <summary>
        /// 移除集成事件监听器
        /// </summary>
        /// <param name="listener">事件监听器</param>
        void RemoveEventListener(IIntegrationEventListener listener);
    }

    /// <summary>
    /// 集成事件监听器接口
    /// </summary>
    public interface IIntegrationEventListener
    {
        /// <summary>
        /// 工作流创建事件
        /// </summary>
        Task OnWorkflowCreatedAsync(string workflowId, PlanningResult planningResult);

        /// <summary>
        /// 工作流状态同步事件
        /// </summary>
        Task OnWorkflowStatusSyncedAsync(string workflowId, string dialogSessionId, WorkflowStatus status);

        /// <summary>
        /// 智能决策执行事件
        /// </summary>
        Task OnSmartDecisionExecutedAsync(SmartDecisionRequest request, SmartDecisionResult result);

        /// <summary>
        /// 集成错误事件
        /// </summary>
        Task OnIntegrationErrorAsync(string source, Exception error);
    }

    /// <summary>
    /// 规划结果模型
    /// </summary>
    public class PlanningResult
    {
        /// <summary>
        /// 规划ID
        /// </summary>
        public string Id { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 用户请求
        /// </summary>
        public string UserRequest { get; set; } = string.Empty;

        /// <summary>
        /// 规划类型
        /// </summary>
        public PlanningType Type { get; set; }

        /// <summary>
        /// 角色规划结果
        /// </summary>
        public CharacterPlanningResult? CharacterPlanning { get; set; }

        /// <summary>
        /// 工作流规划结果
        /// </summary>
        public WorkflowPlanningResult? WorkflowPlanning { get; set; }

        /// <summary>
        /// 质量控制规划结果
        /// </summary>
        public QualityControlPlanningResult? QualityControlPlanning { get; set; }

        /// <summary>
        /// 生成的文件列表
        /// </summary>
        public List<GeneratedFile> GeneratedFiles { get; set; } = new();

        /// <summary>
        /// 规划元数据
        /// </summary>
        public PlanningMetadata Metadata { get; set; } = new();

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; } = DateTime.Now;
    }

    /// <summary>
    /// 规划类型枚举
    /// </summary>
    public enum PlanningType
    {
        CharacterOnly,
        WorkflowOnly,
        QualityControlOnly,
        Comprehensive,
        Custom
    }

    /// <summary>
    /// 角色规划结果
    /// </summary>
    public class CharacterPlanningResult
    {
        /// <summary>
        /// 主要角色列表
        /// </summary>
        public List<CharacterDefinition> MainCharacters { get; set; } = new();

        /// <summary>
        /// 配角列表
        /// </summary>
        public List<CharacterDefinition> SupportingCharacters { get; set; } = new();

        /// <summary>
        /// 角色关系网络
        /// </summary>
        public CharacterRelationshipNetwork RelationshipNetwork { get; set; } = new();

        /// <summary>
        /// 角色发展轨迹
        /// </summary>
        public List<CharacterDevelopmentPath> DevelopmentPaths { get; set; } = new();
    }

    /// <summary>
    /// 工作流规划结果
    /// </summary>
    public class WorkflowPlanningResult
    {
        /// <summary>
        /// 工作流步骤
        /// </summary>
        public List<PlannedWorkflowStep> Steps { get; set; } = new();

        /// <summary>
        /// 执行策略
        /// </summary>
        public ExecutionStrategy Strategy { get; set; } = new();

        /// <summary>
        /// 预估时间
        /// </summary>
        public TimeEstimate TimeEstimate { get; set; } = new();

        /// <summary>
        /// 资源需求
        /// </summary>
        public ResourceRequirements ResourceRequirements { get; set; } = new();
    }

    /// <summary>
    /// 质量控制规划结果
    /// </summary>
    public class QualityControlPlanningResult
    {
        /// <summary>
        /// 质量检查点
        /// </summary>
        public List<QualityCheckpoint> Checkpoints { get; set; } = new();

        /// <summary>
        /// 质量标准
        /// </summary>
        public QualityStandards Standards { get; set; } = new();

        /// <summary>
        /// 审查流程
        /// </summary>
        public ReviewProcess ReviewProcess { get; set; } = new();
    }

    /// <summary>
    /// 生成的文件信息
    /// </summary>
    public class GeneratedFile
    {
        /// <summary>
        /// 文件路径
        /// </summary>
        public string FilePath { get; set; } = string.Empty;

        /// <summary>
        /// 文件类型
        /// </summary>
        public string FileType { get; set; } = string.Empty;

        /// <summary>
        /// 文件大小
        /// </summary>
        public long FileSize { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; } = DateTime.Now;

        /// <summary>
        /// 文件描述
        /// </summary>
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 是否为模板文件
        /// </summary>
        public bool IsTemplate { get; set; } = false;
    }

    /// <summary>
    /// 规划元数据
    /// </summary>
    public class PlanningMetadata
    {
        /// <summary>
        /// 执行时间（毫秒）
        /// </summary>
        public long ExecutionTimeMs { get; set; }

        /// <summary>
        /// 使用的AI模型
        /// </summary>
        public string? AIModel { get; set; }

        /// <summary>
        /// 质量评分
        /// </summary>
        public double QualityScore { get; set; }

        /// <summary>
        /// 复杂度评分
        /// </summary>
        public double ComplexityScore { get; set; }

        /// <summary>
        /// 置信度评分
        /// </summary>
        public double ConfidenceScore { get; set; }

        /// <summary>
        /// 标签
        /// </summary>
        public List<string> Tags { get; set; } = new();

        /// <summary>
        /// 额外属性
        /// </summary>
        public Dictionary<string, object> Properties { get; set; } = new();
    }

    /// <summary>
    /// 工作流创建上下文
    /// </summary>
    public class WorkflowCreationContext
    {
        /// <summary>
        /// 项目ID
        /// </summary>
        public string? ProjectId { get; set; }

        /// <summary>
        /// 用户ID
        /// </summary>
        public string? UserId { get; set; }

        /// <summary>
        /// 对话会话ID
        /// </summary>
        public string? DialogSessionId { get; set; }

        /// <summary>
        /// 创建选项
        /// </summary>
        public WorkflowCreationOptions Options { get; set; } = new();

        /// <summary>
        /// 输入参数
        /// </summary>
        public Dictionary<string, object> InputParameters { get; set; } = new();

        /// <summary>
        /// 环境变量
        /// </summary>
        public Dictionary<string, string> EnvironmentVariables { get; set; } = new();
    }

    /// <summary>
    /// 工作流创建选项
    /// </summary>
    public class WorkflowCreationOptions
    {
        /// <summary>
        /// 是否自动启动
        /// </summary>
        public bool AutoStart { get; set; } = false;

        /// <summary>
        /// 是否启用监控
        /// </summary>
        public bool EnableMonitoring { get; set; } = true;

        /// <summary>
        /// 是否启用通知
        /// </summary>
        public bool EnableNotifications { get; set; } = true;

        /// <summary>
        /// 优先级
        /// </summary>
        public WorkflowPriority Priority { get; set; } = WorkflowPriority.Normal;

        /// <summary>
        /// 超时时间（分钟）
        /// </summary>
        public int TimeoutMinutes { get; set; } = 60;

        /// <summary>
        /// 最大重试次数
        /// </summary>
        public int MaxRetries { get; set; } = 3;
    }

    /// <summary>
    /// 集成执行结果
    /// </summary>
    public class IntegratedExecutionResult
    {
        /// <summary>
        /// 执行ID
        /// </summary>
        public string ExecutionId { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 规划结果
        /// </summary>
        public PlanningResult PlanningResult { get; set; } = new();

        /// <summary>
        /// 创建的工作流
        /// </summary>
        public AgentWorkflow? CreatedWorkflow { get; set; }

        /// <summary>
        /// 执行状态
        /// </summary>
        public IntegratedExecutionStatus Status { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 执行日志
        /// </summary>
        public List<string> ExecutionLogs { get; set; } = new();

        /// <summary>
        /// 错误信息
        /// </summary>
        public List<string> Errors { get; set; } = new();

        /// <summary>
        /// 生成的输出
        /// </summary>
        public Dictionary<string, object> Outputs { get; set; } = new();
    }

    /// <summary>
    /// 集成执行状态枚举
    /// </summary>
    public enum IntegratedExecutionStatus
    {
        Planning,
        WorkflowCreation,
        WorkflowExecution,
        Completed,
        Failed,
        Cancelled
    }

    /// <summary>
    /// 集成进度信息
    /// </summary>
    public class IntegratedProgress
    {
        /// <summary>
        /// 执行ID
        /// </summary>
        public string ExecutionId { get; set; } = string.Empty;

        /// <summary>
        /// 当前阶段
        /// </summary>
        public IntegratedExecutionStatus CurrentStage { get; set; }

        /// <summary>
        /// 总体进度百分比
        /// </summary>
        public int OverallProgressPercentage { get; set; }

        /// <summary>
        /// 当前阶段进度百分比
        /// </summary>
        public int StageProgressPercentage { get; set; }

        /// <summary>
        /// 状态消息
        /// </summary>
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 详细信息
        /// </summary>
        public string? Details { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        public DateTime Timestamp { get; set; } = DateTime.Now;
    }

    /// <summary>
    /// 工作流控制命令枚举
    /// </summary>
    public enum WorkflowControlCommand
    {
        Start,
        Pause,
        Resume,
        Stop,
        Cancel,
        Restart,
        Skip,
        Retry
    }

    /// <summary>
    /// 工作流控制结果
    /// </summary>
    public class WorkflowControlResult
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 结果消息
        /// </summary>
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 新的工作流状态
        /// </summary>
        public WorkflowStatus? NewStatus { get; set; }

        /// <summary>
        /// 执行时间
        /// </summary>
        public DateTime ExecutionTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 额外信息
        /// </summary>
        public Dictionary<string, object> AdditionalInfo { get; set; } = new();
    }
}
